package com.tutu.service.impl;

import com.tutu.controller.backend.FunctionalModuleController;
import com.tutu.core.Constant;
import com.tutu.core.api.CompanyFunctionalModuleApiUrls;
import com.tutu.core.api.FunctionalModuleApiUrls;
import com.tutu.service.FunctionalModuleService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import com.tutu.utils.UnicodeUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class FunctionalModuleServiceImpl implements FunctionalModuleService {

    @Override
    public Map<String, Object> pageByParams(Integer type, String keyWord, Integer pageNum, Integer pageSize) {
        Map<String, Object> result = HttpRequestUtil.getOnlyData(FunctionalModuleApiUrls.base_prefix + FunctionalModuleApiUrls.page, "type=" + type + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);

        if ((result.get("content") + "").length() > 0) {
            Map<String, Object> tempMap;
            FunctionalModuleController.functionalModuleMap.clear();

            for (Object o : JSONArray.fromObject(result.get("content"))) {
                tempMap = JsonUtil.json2Map(o);

                FunctionalModuleController.functionalModuleMap.put(tempMap.get("id") + "", tempMap);
            }
        }

        return result;
    }

    @Override
    public Map<String, Object> pageChildByParams(Integer functionalModuleId, String keyWord, Integer type, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(CompanyFunctionalModuleApiUrls.base_prefix + CompanyFunctionalModuleApiUrls.companyPage, "functionalModuleId=" + functionalModuleId + "&type=" + type + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, Integer type, String name) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑功能模块信息，ID：" + id + "，类型：" + type + "，名称：" + name);

        return HttpRequestUtil.getCompleteData(FunctionalModuleApiUrls.base_prefix + FunctionalModuleApiUrls.update, "id=" + id + "&type=" + type + "&name=" + UnicodeUtil.unicodeEncode(name));
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除功能模块信息，ID：" + id);

        return HttpRequestUtil.getCompleteData(FunctionalModuleApiUrls.base_prefix + FunctionalModuleApiUrls.delete, "id=" + id);
    }

    @Override
    public Map<String, Object> updateInfo(HttpServletRequest request, Integer functionalModuleId, Integer companyId, Integer type) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑公司功能模块关联信息，功能模块ID：" + functionalModuleId + "，公司ID：" + companyId + "，类型：" + type);

        String url = CompanyFunctionalModuleApiUrls.base_prefix;

        if (null == type || type.equals(Constant.CHECK_STATUS_DEFAULT)) {
            // 添加
            url += CompanyFunctionalModuleApiUrls.create;
        } else {
            url += CompanyFunctionalModuleApiUrls.delete;
        }

        return HttpRequestUtil.getCompleteData(url, "functionalModuleId=" + functionalModuleId + "&companyId=" + companyId);
    }
}
